//
//  NSDecimalNumber+EasyCalc.m
//  CarRentalApp
//
//  Created by James on 2021/9/15.
//

#import "NSDecimalNumber+EasyCalc.h"

@implementation NSDecimalNumber (EasyCalc)

///数组累和
+(NSDecimalNumber *)decimalNumberAddingWithArray:(NSArray *)array
                                    withBehavior:(id)behavior{
    
    
    
    NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:@"0"];
    
    if(array.count == 0){ return number; }

    /*
         NSRoundingMode: 解释
         NSRoundPlain    是四舍五入
         NSRoundDown     是向下取整
         NSRoundUp       是向上取整
         NSRoundBankers  是在四舍五入的基础上，当末尾数为5时，如果前一位是偶数,不变; 前一位是奇数,+1变成偶数.
         */
    //四舍五入,结果保留两位小数
    
    if(behavior == nil){
        behavior = [[NSDecimalNumberHandler alloc] initWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
    }

    for (id value in array) {
        if([value isKindOfClass:[NSString class]]){
            if([(NSString *)value length] == 0){
                continue;
            }
            number = [number decimalNumberByAdding:[NSDecimalNumber decimalNumberWithString:value] withBehavior:behavior];
        }else if ([value isKindOfClass:[NSDecimalNumber class]]){
            number = [number decimalNumberByAdding:value withBehavior:behavior];
        }else if ([value isKindOfClass:[NSNumber class]]){
            number = [number decimalNumberByAdding:[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%@",value]] withBehavior:behavior];
        }
    }
    
    return number;
}

+(NSDecimalNumber *)decimalNumberMultiWith:(id)value1
                                    value2:(id)value2
                              withBehavior:(id)behavior{
    NSDecimalNumber *newValue1;
    NSDecimalNumber *newValue2;
    
    if([value1 isKindOfClass:[NSString class]]){
        if([(NSString *)value1 length] == 0){
            return [NSDecimalNumber decimalNumberWithString:@"0"];
        }
        newValue1 = [NSDecimalNumber decimalNumberWithString:value1];
       
    }else if ([value1 isKindOfClass:[NSDecimalNumber class]]){
        newValue1 = value1;
    }else if ([value1 isKindOfClass:[NSNumber class]]){
        newValue1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%@",value1]];
    }
    
    if([value2 isKindOfClass:[NSString class]]){
        if([(NSString *)value2 length] == 0){
            return [NSDecimalNumber decimalNumberWithString:@"0"];
        }
        newValue2 = [NSDecimalNumber decimalNumberWithString:value2];
    }else if ([value2 isKindOfClass:[NSDecimalNumber class]]){
        newValue2 = value2;
    }else if ([value2 isKindOfClass:[NSNumber class]]){
        newValue2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%@",value2]];
    }
    if(behavior == nil){
        behavior = [[NSDecimalNumberHandler alloc] initWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
    }
    
    return [newValue1 decimalNumberByMultiplyingBy:newValue2 withBehavior:behavior];
}

/// 两个数相除
/// @param value1 除数 （可接受字符串  NSNumber   NSDecimalNumber）
/// @param value2 被除数 （可接受字符串  NSNumber   NSDecimalNumber）
/// @param behavior <#behavior description#>
+(NSDecimalNumber *)decimalNumberDivisionWith:(id)value1
                                       value2:(id)value2
                                 withBehavior:(nullable id)behavior{
    NSDecimalNumber *newValue1;
    NSDecimalNumber *newValue2;
    
    if([value1 isKindOfClass:[NSString class]]){
        if([(NSString *)value1 length] == 0){
            return [NSDecimalNumber decimalNumberWithString:@"0"];
        }
        newValue1 = [NSDecimalNumber decimalNumberWithString:value1];
    }else if ([value1 isKindOfClass:[NSDecimalNumber class]]){
        newValue1 = value1;
    }else if ([value1 isKindOfClass:[NSNumber class]]){
        newValue1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%@",value1]];
    }
    
    if([value2 isKindOfClass:[NSString class]]){
        if([(NSString *)value2 length] == 0){
            return [NSDecimalNumber decimalNumberWithString:@"0"];
        }
        newValue2 = [NSDecimalNumber decimalNumberWithString:value2];
    }else if ([value2 isKindOfClass:[NSDecimalNumber class]]){
        newValue2 = value2;
    }else if ([value2 isKindOfClass:[NSNumber class]]){
        newValue2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%@",value2]];
    }
    if(behavior == nil){
        behavior = [[NSDecimalNumberHandler alloc] initWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
    }
    
    return [newValue1 decimalNumberByDividingBy:newValue2 withBehavior:behavior];
}

+(NSDecimalNumber *)decimalNumberWithAddingArr:(NSArray *)array
                                    scale:(id)scale
                                  withBehavior:(nullable id)behavior{
    if(behavior == nil){
        behavior = [[NSDecimalNumberHandler alloc] initWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
    }
    
    NSDecimalNumber *number = [self decimalNumberAddingWithArray:array withBehavior:behavior];
    number = [self decimalNumberMultiWith:number value2:scale withBehavior:behavior];
    
    return number;
}

-(NSString *)priceTypeString{
    NSString *str = self.stringValue;
    if([str containsString:@"."]){
        
        NSString *lastStr = [str componentsSeparatedByString:@"."].lastObject;
        if(lastStr.length == 1){
            str = [str stringByAppendingString:@"0"];
        }else if(lastStr.length == 0){
            str = [str stringByAppendingString:@".00"];
        }
        
        
    }else{
        str = [str stringByAppendingString:@".00"];
    }
    return str;
}

@end
